package com.itheima.appserver.controller;


import cn.hutool.core.convert.Convert;
import com.itheima.appserver.config.MyCache;
import com.itheima.appserver.service.CommentService;
import com.itheima.appserver.service.PublishService;
import com.itheima.commons.pojo.Comment;
import com.itheima.commons.pojo.Publish;
import com.itheima.commons.vo.PageResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    PublishService publishService;

    @Autowired
    CommentService commentService;

    //推荐动态
    @GetMapping("/recommend")
    public ResponseEntity recommendMovements(@RequestParam Integer page,Integer pagesize){
        PageResult pageResult =publishService.recommendMovements(page,pagesize, Comment.class);
        return ResponseEntity.ok(pageResult);
    }

    //好友动态
    @MyCache(cacheTime = 100L)
    @GetMapping
    public ResponseEntity friendsMovements(@RequestParam Integer page,Integer pagesize){
        PageResult pageResult = publishService.friendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
    //动态-发布
    @PostMapping
    public ResponseEntity sendMovements(@RequestParam Map<String,Object> params,
                                        @RequestParam(value = "imageContent",required = false) MultipartFile[] multipartFile) throws IOException {
        publishService.sendMovements(params,multipartFile);
        return ResponseEntity.ok(null);
    }
    //点赞
    @GetMapping("/{id}/like")
    public ResponseEntity likeMovements(@PathVariable("id")String publishid) {
        commentService.comment(publishid,1,Comment.class);
        return ResponseEntity.ok(null);
    }
    //取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeMovements(@PathVariable("id")String publishid) {
        commentService.disComment(publishid,1,Comment.class);
        return ResponseEntity.ok(null);
    }
    //喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity loveMovements(@PathVariable("id")String publishid) {
        commentService.comment(publishid,3,Comment.class);
        return ResponseEntity.ok(null);
    }
    //取消喜欢
    @GetMapping("/{id}/dislove")
    public ResponseEntity disloveMovements(@PathVariable("id")String publishid) {
        commentService.disComment(publishid,3, Comment.class);
        return ResponseEntity.ok(null);
    }

    //用户动态所有
    //@GetMapping("/all")
   /* public ResponseEntity allFriendsMovements(@RequestParam Integer page,Integer pagesize){
        PageResult pageResult = publishService.friendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }*/
    //单条动态
   @GetMapping("{id}")
    public ResponseEntity oneMovements(@PathVariable("id")ObjectId publishid) {
       publishService.queryById(publishid);
       return ResponseEntity.ok(null);
   }


    /**
     * 谁看过我
     * movements/visitors
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        return ResponseEntity.ok(null);
    }

    /**
     * 自己的所有动态
     * @param page
     * @param pageSize
     * @param userId   要查询的那个人的ID  不是登录人的ID
     * @return
     */
    @GetMapping("all")
    public ResponseEntity queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "userId") Long userId) {


        PageResult pageResult = publishService.queryAlbumList(userId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }
}
